/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.entity;

import vip.dexian.common.utils.LocalDateTimeUtils;
import vip.dexian.core.base.bean.BaseEntity;
import vip.dexian.core.base.bean.BaseLogEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Index;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Where;

import java.io.Serial;
import java.time.LocalDateTime;

/**
 * 管理员登录日志
 *
 * @author 挺好的 2023年06月07日 10:45
 */
@Entity
@Table (name = "br_admin_login_log", indexes = {@Index (name = "is_valid", columnList = "isValid"),})
@DynamicUpdate
@Where (clause = BaseEntity.IS_VALID_WHERE)
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode (callSuper = true)
public class LoginLog extends BaseLogEntity <Long> {

    @Serial
    private static final long serialVersionUID = 3587340125228280125L;

    /**
     * 登录用户名
     */
    @Comment ("用户名")
    @Column (nullable = false, length = 20)
    private String username;

    /**
     * 登录密码，登录成功之后，不保存密码
     */
    @Comment ("密码")
    @Column (length = 60)
    @ToString.Exclude
    private String password;

    /**
     * 登录是否成功
     */
    @Comment ("是否成功")
    @Column (nullable = false)
    private Boolean isSuccess;

    /**
     * 可删除时间
     */
    @Comment ("可删除时间")
    @Column (nullable = false)
    private LocalDateTime canDeleteDate;

    /**
     * 备注信息
     */
    @Comment ("备注")
    @Column (length = 1000)
    private String memo;

    /**
     * @return 是否允许删除
     */
    @Transient
    public Boolean getIsAllowedDelete () {
        return LocalDateTimeUtils.isAfter(LocalDateTime.now(), this.canDeleteDate);
    }

    /**
     * 保存前的操作
     */
    @PrePersist
    public void prePersist () {
        // 设置15天后才可以删除
        this.setCanDeleteDate(LocalDateTime.now().plusDays(15L));
    }

}
